package com.klm.easymq.controller;

import com.klm.easymq.metrics.EasyMqMetrics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Easy-MQ 指标控制器
 * 提供指标查询和统计接口
 */
@Slf4j
@RestController
@RequestMapping("/easymq/api/metrics")
public class EasyMqMetricsController {
    
    @Autowired
    private EasyMqMetrics easyMqMetrics;
    
    /**
     * 获取指标统计摘要
     */
    @GetMapping("/summary")
    public Map<String, Object> getMetricsSummary() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> summary = easyMqMetrics.getMetricsSummary();
            result.put("success", true);
            result.put("data", summary);
            result.put("timestamp", System.currentTimeMillis());
        } catch (Exception e) {
            log.error("获取指标统计失败", e);
            result.put("success", false);
            result.put("message", "获取指标统计失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 获取消息发送统计
     */
    @GetMapping("/messages/sent")
    public Map<String, Object> getMessageSentStats() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> summary = easyMqMetrics.getMetricsSummary();
            Map<String, Object> counters = (Map<String, Object>) summary.get("counters");
            
            Map<String, Object> sentStats = new HashMap<>();
            if (counters != null) {
                // 提取消息发送相关的计数器
                for (Map.Entry<String, Object> entry : counters.entrySet()) {
                    String key = entry.getKey();
                    if (key.contains("messages_sent")) {
                        sentStats.put(key, entry.getValue());
                    }
                }
            }
            
            result.put("success", true);
            result.put("data", sentStats);
            result.put("timestamp", System.currentTimeMillis());
        } catch (Exception e) {
            log.error("获取消息发送统计失败", e);
            result.put("success", false);
            result.put("message", "获取消息发送统计失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 获取消息消费统计
     */
    @GetMapping("/messages/consumed")
    public Map<String, Object> getMessageConsumedStats() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> summary = easyMqMetrics.getMetricsSummary();
            Map<String, Object> counters = (Map<String, Object>) summary.get("counters");
            
            Map<String, Object> consumedStats = new HashMap<>();
            if (counters != null) {
                // 提取消息消费相关的计数器
                for (Map.Entry<String, Object> entry : counters.entrySet()) {
                    String key = entry.getKey();
                    if (key.contains("messages_consumed")) {
                        consumedStats.put(key, entry.getValue());
                    }
                }
            }
            
            result.put("success", true);
            result.put("data", consumedStats);
            result.put("timestamp", System.currentTimeMillis());
        } catch (Exception e) {
            log.error("获取消息消费统计失败", e);
            result.put("success", false);
            result.put("message", "获取消息消费统计失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 获取错误统计
     */
    @GetMapping("/errors")
    public Map<String, Object> getErrorStats() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> summary = easyMqMetrics.getMetricsSummary();
            Map<String, Object> counters = (Map<String, Object>) summary.get("counters");
            
            Map<String, Object> errorStats = new HashMap<>();
            if (counters != null) {
                // 提取错误相关的计数器
                for (Map.Entry<String, Object> entry : counters.entrySet()) {
                    String key = entry.getKey();
                    if (key.contains("errors") || key.contains("retries")) {
                        errorStats.put(key, entry.getValue());
                    }
                }
            }
            
            result.put("success", true);
            result.put("data", errorStats);
            result.put("timestamp", System.currentTimeMillis());
        } catch (Exception e) {
            log.error("获取错误统计失败", e);
            result.put("success", false);
            result.put("message", "获取错误统计失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 获取性能统计
     */
    @GetMapping("/performance")
    public Map<String, Object> getPerformanceStats() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> summary = easyMqMetrics.getMetricsSummary();
            Map<String, Object> timers = (Map<String, Object>) summary.get("timers");
            
            result.put("success", true);
            result.put("data", timers);
            result.put("timestamp", System.currentTimeMillis());
        } catch (Exception e) {
            log.error("获取性能统计失败", e);
            result.put("success", false);
            result.put("message", "获取性能统计失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 获取连接状态
     */
    @GetMapping("/connections")
    public Map<String, Object> getConnectionStats() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> summary = easyMqMetrics.getMetricsSummary();
            Map<String, Object> gauges = (Map<String, Object>) summary.get("gauges");
            
            Map<String, Object> connectionStats = new HashMap<>();
            if (gauges != null) {
                // 提取连接状态相关的仪表
                for (Map.Entry<String, Object> entry : gauges.entrySet()) {
                    String key = entry.getKey();
                    if (key.contains("connection_status")) {
                        connectionStats.put(key, entry.getValue());
                    }
                }
            }
            
            result.put("success", true);
            result.put("data", connectionStats);
            result.put("timestamp", System.currentTimeMillis());
        } catch (Exception e) {
            log.error("获取连接状态失败", e);
            result.put("success", false);
            result.put("message", "获取连接状态失败: " + e.getMessage());
        }
        return result;
    }
} 